<!DOCTYPE html>
<html>

<head>
    <title>编辑游记</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css" />
    <link rel="stylesheet" href="/js/plugins/chosen/component-chosen.css" />
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/plugins/messager/jquery.bootstrap.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/plugins/chosen/chosen.jquery.js"></script>
    <script>
        $(function () {
            //富编辑框的使用---------------------------------------
            var Edr = new Eleditor({
                //编辑框出现的位置
                el: '#contentEditor',
                /*发送的url*/
                upload:{server: '/images'},
            });
            //end-----------------------------------------------
            //上传封面----------------------------------------------
            $("#changeBtn").click(function () {
                //是input元素的id
                $("#fileBtn").click();
            })
            //监听input内容改变
            $("#fileBtn").change(function () {
                if ($(this).val()){
                    //异步提交表单
                    $("#fileForm").ajaxSubmit(function (data) {
                        if (data.status == 1) {
                            //attr保存的是相对路径,没有http://
                            //prop获得的是upload路径
                            $("#coverUrl").attr("src", data.url)
                        }else {
                            $(document).dialog({
                                autoClose: 3000,
                                content: "上传失败"
                            });
                        }
                    })
                }
            })
            //end----------------------------------------------
            //地区操作列表的回显

            $.get("/regions",function (data) {
                //<option value="1">广州</option>
                //将返回的数据拼接到option中
                var html="";
                $.each(data,function (index, item) {
                    html += '<option value="'+item.id+'">'+item.name+'</option>'
                })
                $("#regions").html(html);
                //chosen插件
                $('#regions').chosen({
                    disable_search_threshold: 10,
                    no_results_text: '无相关地区',//搜索无结果时显示的提示
                    search_contains:true
                });
                //end---------------------------------------------------------------


                //编辑游记

                //获取id,回显指定的游记
                var params = getParams();//{id:1} //从请求头中获取游记的id
                var id = params.id;
                //编辑指定游记时的回显
                if (id){
                $.get("/travels/"+id,function (data) {
                    $(".travels").renderValues(data); //渲染
                    $("#detailForm input[name='_method']").val("PUT"); //渲染完之后给页面动态添加put
                    //回显
                    $('#regions').trigger("chosen:updated");//chosen差价
                })
                }
                //end----------------------------------
            })

            //end------------------------------------------------------

            $("#save").click(function () {

                //保存操作 和发布操作/待审核状态
                var state = $(this).data("state");
                //添加表单时需要提交额外参数:在发送ajax请求之前,需要在form中添加需要保存的全部数据
                $("#detailForm").ajaxSubmit({
                    //提交表单之前需要执行的参数
                    //第一个参数是数组形式的,表示提交表单的内容
                    //第二个参数要提交的form的表单
                    beforeSubmit:function (params,form,options) {
                        //手动的将需要提交的表单中添加保存时需要的参数,只有是input元素
                        params.push({name:"state",value:state});
                        params.push({name:"coverUrl",value:$('#coverUrl').attr("src")});
                        params.push({name:"title",value:$('#title').val()});
                        //获得文章的具体内容
                        params.push({name:"travelContent.content",value:Edr.getContent()});
                        console.log(params);
                    },

                    //表示成功调教表单之后执行的函数
                    success:function (data) {
                        if (data.success){
                            window.location.href="/mine/travels.html";
                        }else {
                            window.messager.alert("温馨提示,",e.meg);
                        }
                    }
                })
            })
            //end---------------------------------------


        })
    </script>

</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1);">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-floppy-o fg-fw" id="save" data-state="1"><span>保存</span></i>
            <i class="fa fa-check" id="release" data-state="2"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <!--上传封面的表单空间-->
    <form action="/images" method="post" enctype="multipart/form-data" id="fileForm">
        <input type="file" name="file" accept="image/*" style="display: none" id="fileBtn">
    </form>
    <div class="cover">
        <img id="coverUrl" render-src="coverUrl" name="coverUrl" width="100%" height="200px" />
        <form id="coverForm" method="post" enctype="multipart/form-data">
            <input type="file" name="pic" style="display: none" id="fileInputBtn" accept="image/gif,image/jpeg,image/jpg,image/png"/>
            <button type="button" class="btn" id="changeBtn">
                <i class="fa fa-pencil-square-o"></i>
                <small>更换封面</small>
            </button>
        </form>
    </div>
    <div class="title">
        <input class="form-control" render-value="title" name="title" id="title"  placeholder="请输入标题"/>
    </div>
   <!--<h6 href="#">点击文章内容即可编辑</h6>-->
    <div id="contentEditor" render-html="travelContent.content">
    </div>
    <hr/>
    <div class="container">
        <form id="detailForm" method="post" action="/travels">
            <input name="_method" type="hidden"> <!--编辑的方式 value=put-->
            <input type="hidden" name="id" render-value="id"/>
            <label>出行时间:</label>
            <input type="text" name="travelTime" onclick="WdatePicker()" render-value="travelTime" readonly class="form-control" >
            <label>出行天数:</label>
            <input type="number" render-value="days" name="days" class="form-control">
            <label>参与人物:</label>
            <select class="c-select form-control" name="person" render-value="person">
                <option value="1" selected>一个人的旅行</option>
                <option value="2">和父母</option>
                <option value="3">和朋友</option>
                <option value="4">和同事</option>
                <option value="5">和爱人</option>
                <option value="6">和其他</option>
            </select>
            <label>人均费用:</label>
            <input type="text" name="perExpends" class="form-control" render-value="perExpends">

            <label>旅游地点:</label>
            <!--<select  class="form-control" id="regions" name="place.id" render-value="place.id">-->
            <select data-placeholder="请选择旅游地点" class="chosen-select" tabindex="2" id="regions" name="place.id" render-value="place.id">
                <option value="1">广州</option>
            </select>

            <div class="form-check">
                <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" render-attr="checked=isPublic" name="isPublic">是否公开
                </label>
            </div>
        </form>
    </div>

    <hr/>

</div>


</body>

</html>